package com.haotunet.app.goldtreasure.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.goldplusgold.imageloader.lib.GlideRoundTransform;
import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.activity.DynamicDetailActivity;
import com.haotunet.app.goldtreasure.model.MediaInfo;

import java.util.List;

public class MediaAdapter extends HolderAdapter<MediaInfo, MediaAdapter.ViewHolder> {

    public MediaAdapter(Context context, List<MediaInfo> mlist) {
        super(context, mlist);
    }

    public static class ViewHolder {
        TextView tvCreateTime;
        ImageView ivMediaImage;
        TextView tvTitle;
    }

    @Override
    public View buildConvertView(LayoutInflater layoutInflater, MediaInfo t, int position) {
        return inflate(R.layout.item_media_coverage);
    }

    @Override
    public ViewHolder buildHolder(View convertView, MediaInfo t, int position) {
        ViewHolder holder = new ViewHolder();
        holder.tvTitle = (TextView) convertView.findViewById(R.id.media_title);
        holder.ivMediaImage = (ImageView) convertView.findViewById(R.id.media_image);
        holder.tvCreateTime = (TextView) convertView.findViewById(R.id.create_time_txt);
        return holder;
    }

    @Override
    public void bindViewDatas(View convertView, ViewHolder holder, final MediaInfo mediaInfo, int position) {
        if (TextUtils.isEmpty(mediaInfo.getImageUrl().trim())) {
            holder.ivMediaImage.setVisibility(View.GONE);
        } else {
            Glide.with(context).load(mediaInfo.getImageUrl().trim()).placeholder(R.drawable.placeholder_media_find)
                    .error(R.drawable.placeholder_media_find).transform(new GlideRoundTransform(context, 4)).into(holder.ivMediaImage);
        }
        holder.tvCreateTime.setText(mediaInfo.getIssuanceTime());
        holder.tvTitle.setText(mediaInfo.getTitle());
        convertView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, DynamicDetailActivity.class);
                intent.putExtra("activityUrl", mediaInfo.getLinkUrl());
                intent.putExtra("type", 10);
                context.startActivity(intent);
            }
        });

    }
}
